package com.youease.chris.view.mediator
{
	import com.youease.chris.manager.GlobalObject;
	import com.youease.chris.manager.OperateEnum;
	import com.youease.chris.manager.TexturesManager;
	import com.youease.chris.model.vo.BigImageData;
	import com.youease.chris.model.vo.TextureData;
	import com.youease.chris.utils.CutRhombus;
	import com.youease.chris.view.TextureCutTitleWindow;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.events.ListEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class TextureCutMediator extends Mediator
	{
		public static const NAME:String = "TextureCutMediator";
		public static const TEXTURE_CUT:String = "TextureCut";
		
		private var fr:File;
		
		public function TextureCutMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			textureCut.add.addEventListener(MouseEvent.CLICK,onAdd);
			textureCut.choose.addEventListener(MouseEvent.CLICK,onChoose);
			textureCut.cutList.addEventListener(ListEvent.ITEM_CLICK,onSelectTexture);
			textureCut.del.addEventListener(MouseEvent.CLICK,onDel);
		}
		
		private function onDel(evt:MouseEvent):void {
			var selected:BigImageData = TexturesManager.bigTextures[textureCut.cutList.selectedItem];
			if(selected) {
				
				var row:int = Math.ceil(selected.height/GlobalObject.cellHeight)*2 + 1;
				var col:int = Math.ceil(selected.width/GlobalObject.cellWidth) + 1;
				var name:String = selected.name.substring(0,selected.name.indexOf("."));

				for(var r:int=0;r<row;r++) {
					for(var c:int=0;c<col;c++) {
						if(TexturesManager.textures[name+"_"+r+"_"+c+".gif"])
							delete TexturesManager.textures[name+"_"+r+"_"+c+".gif"];
					}
				}
				
				var directory:String = GlobalObject.projectDir+"/textures/"+selected.name.substring(0,selected.name.indexOf("/"));
				var file:File = new File(directory);
				file.deleteDirectory(true);
				
				delete TexturesManager.bigTextures[textureCut.cutList.selectedItem];
				this.sendNotification(TEXTURE_CUT);
				this.textureCut.img.source = null;
			}
		}
		
		private function onSelectTexture(evt:ListEvent):void {
			textureCut.img.source = GlobalObject.projectDir+"/textures/"+TexturesManager.bigTextures[textureCut.cutList.selectedItem]["name"];
		}
		
		private function onAdd(evt:MouseEvent):void {
			var ff:FileFilter = new FileFilter("图片","*.jpg;*.gif;*.png;*.bmp,*.jpeg");
			fr = new File();
			fr.addEventListener(Event.SELECT,onSelect);
			fr.browse([ff]);
		}
		
		public function onSelect(evt:Event):void {
			fr.removeEventListener(Event.SELECT,onSelect);
			fr.addEventListener(Event.COMPLETE,onLoadComplete);
			fr.load();
		}
		
		public function onLoadComplete(evt:Event):void {
			fr = evt.target as File;
			var loader:Loader = new Loader();
			loader.unload();
			loader.loadBytes(fr.data);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, image_completeHandler(fr.name));
			
			writeImage(fr.name,fr.data);
		}
		
		public function image_completeHandler(fileName:String):Function {
			var func:Function = function(evt:Event):void {
				
				new CutRhombus().cut(fileName,Bitmap(evt.currentTarget.content).bitmapData);
				TexturesManager.bigTextures[fileName] = new BigImageData(fileName.substring(0,fileName.indexOf("."))+"/"+fileName,Bitmap(evt.currentTarget.content).width,Bitmap(evt.currentTarget.content).height);
				sendNotify();
			}
			return func;
		}
		
		private function sendNotify():void {
			this.sendNotification(TEXTURE_CUT);
		}
		
		private function onChoose(evt:MouseEvent):void {
			if(textureCut.cutList.selectedItem) {
				textureCut.closeWindow();
				GlobalObject.currentBigImag = TexturesManager.bigTextures[textureCut.cutList.selectedItem];
				GlobalObject.currentOperater = OperateEnum.BIG_BRUSH;
			}
		}
		
		override public function listNotificationInterests():Array {
			return [
				TEXTURE_CUT
			];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case TEXTURE_CUT:
					var arr:ArrayCollection = new ArrayCollection();
					for(var name:String in TexturesManager.bigTextures) {
						arr.addItem(name);
					}
					arr.source.sort();
					textureCut.cutList.dataProvider = arr;
					break;
			}
		}
		
		public function writeImage(name:String,bytes:ByteArray):void {
			var fs:FileStream = new FileStream();
			fr.nativePath = GlobalObject.projectDir+"/textures/"+name.substring(0,name.indexOf("."))+"/"+name;
			fs.open(fr,FileMode.WRITE);
			fs.writeBytes(bytes);
			fs.close();
		}
		
		public function get textureCut():TextureCutTitleWindow {
			return this.viewComponent as TextureCutTitleWindow;
		}
		
	}
}